

from utils import *

import random
import socket
import sys
from types import *

class Spaceship:
    accel = 0.0
    alpha = 0.0
    x = 0.0
    y = 0.0
    theta = 0.0
    u = 0.0
    v = 0.0
    omega = 0.0

def response(msg, myname, myrole, debug=False):
    if debug:
        print msg
    if msg == 'win':
        print 'WIN!'
    if msg == 'lose':
        print 'LOSE!'
    if msg in ['miss','hit','win','lose']:
        return None
    me = Spaceship()
    you = Spaceship()
    if myrole == 'observer':
        return '{}'
    me.x, me.y, me.theta, me.u, me.v, me.omega, you.x, you.y, you.theta, you.u, you.v, you.omega = map(float,msg.split(','))
    #now 'me' and 'you' contain all the state info the server will give us
    
    #useless program for now :D
    accel = random.gauss(mu=0.5,sigma=0.5)
    alpha = random.gauss(mu=0.0,sigma=1.0)
    return '{%f,%f}'%(accel, alpha)

if __name__ == '__main__':
    debug=False
    try:
        if '--debug' in sys.argv:
            debug=True
        myname = sys.argv[1]
        myrole = sys.argv[2]
        sa = sys.argv[3]
        ci = sa.index(':')
        si = sa.index('/')
        serverhost = sa[:ci]
        serverport = int(sa[ci+1:si])
        assert (serverport >= 0 and serverport < 2**16)
        servergame = sa[si+1:]
    except:
        print 'expecting command and arguments like:'
        print '  python client.py playername playerrole hostname:portnumber/gamename'
        exit(-1)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((serverhost,serverport))
    sure_send(sock, '{%s}'%(myname))
    sure_recv(sock, '{')
    games = tuplify(2, delim_recv(sock, ['}']).split(','))
    sure_send(sock, '{%s,%s,%s}'%(servergame,'pursuitv0.0',myrole))
    while(1):
        sure_recv(sock, '{')
        msg = delim_recv(sock, ['}'])
        msg = response(msg, myname, myrole, debug=debug)
        if msg:
            sure_send(sock, msg)
        else:
            sure_send(sock, '{1,1}')
            exit(0)
    
